//
//  DiscoverViewController.swift
//  倔强的大轮胎
//
//  Created by Eiji on 17/3/16.
//  Copyright © 2017年 张迎港. All rights reserved.
//  5ANBZ-DTUAW-GMDR4-R4RI2-4T6LE-RFFTN

import UIKit



class DiscoverViewController: UIViewController,UITableViewDelegate,UITableViewDataSource,MAMapViewDelegate {
    var mapView : MAMapView!
    var search : AMapSearchAPI!
    var tableV : UIView!
    var myview : DiscoverView!
    var citys = Array<String>()
    var cityBtn :UIButton! = nil
    
    override func viewDidLoad() {
        super.viewDidLoad()

        title = "发现"
      
        self.navigationController?.isNavigationBarHidden = true
        self.view.backgroundColor = #colorLiteral(red: 1, green: 1, blue: 1, alpha: 1)
        
        creatView()
        
        AMapServices.shared().apiKey = "9327b87ea9894167e0fe86a357f6decb"
        //创建
        createinfo()
        initMapView()
        initSearch()
        createTable()
       
        
        
        //读取plist
//        citys = NSArray(contentsOfFile: NSHomeDirectory() + "/Documents/City.plist")
        
//        print("----------",plistPath!)
//        
        
        
        
        
        
        
        
        
    }
    //获取路径
    func createinfo() {
//        //获取路径
//        let plistPath = Bundle.main.path(forResource: "City.plist", ofType: nil)
//        citys = NSArray.init(contentsOfFile: plistPath!)! as! Array<Any>
//        print("---------",citys)

    }
    //创建表格
    func createTable() {
        //主视图
         tableV = UIView(frame:CGRect(x: 0, y: UIScreen.main.bounds.size.height, width: UIScreen.main.bounds.width, height: UIScreen.main.bounds.size.height))
        tableV.backgroundColor = UIColor.clear
        
        //上面的视图
        let lab1 = UIView(frame: CGRect(x: 0, y: 20, width: UIScreen.main.bounds.size.width, height: 30))
        lab1.backgroundColor = #colorLiteral(red: 1, green: 1, blue: 1, alpha: 1)
        //取消按钮
        let cancelBtn = UIButton(frame: CGRect(x: UIScreen.main.bounds.size.width-70, y: 5, width: 50, height: 20))
        
        cancelBtn.setTitle("取消", for: .init(rawValue: 0))
        cancelBtn.titleLabel?.font = UIFont.systemFont(ofSize: 12)
        cancelBtn.setTitleColor(#colorLiteral(red: 0, green: 0, blue: 0, alpha: 1), for: .init(rawValue: 0))
        cancelBtn.addTarget(nil, action: #selector(cancel), for: .touchUpInside)
//        //Uisearch
//        let search = UISearchController()
        
        
        
        //把按钮加到lab上
        lab1.addSubview(cancelBtn)
        //在lab加到主视图上
        tableV.addSubview(lab1)
        //主视图加到View上
        view.addSubview(tableV)
        
        
        
        
        
        
        let table  = UITableView(frame:CGRect(x: 10, y: 70, width: UIScreen.main.bounds.width-20, height: UIScreen.main.bounds.size.height-70))
        table.delegate = self
        table.dataSource = self
        
        citys = ["北京市","广州市","成都市","深圳市","杭州市","武汉市"]
        
        
        
        tableV.addSubview(table)
        
    }
    //创建View
    func creatView() {
        myview = DiscoverView()
    
        cityBtn = UIButton(frame: CGRect(x: (UIScreen.main.bounds.size.width-30)/2, y: 3, width: 60, height: 20))
        
        cityBtn.setTitle("北京市", for: UIControlState(rawValue: 0))
        cityBtn.setTitleColor(#colorLiteral(red: 0, green: 0, blue: 0, alpha: 1), for: .init(rawValue: 0))
        cityBtn.titleLabel?.font = UIFont.systemFont(ofSize: 13)
        cityBtn.addTarget(nil, action:#selector(clickBtn) , for: .touchUpInside)
        myview.addSubview(cityBtn)
        
        
        view.addSubview(myview)
    }
    //点击城市按钮方法
    func clickBtn(_ but :UIButton) {
        //
        UIView.beginAnimations("ZYG", context: nil)
        UIView.setAnimationDuration(0.5)
        
        tableV.frame = CGRect(x: 0, y: 0, width: UIScreen.main.bounds.size.width, height: UIScreen.main.bounds.size.height)
        myview.frame = CGRect(x: 0, y: -30, width: UIScreen.main.bounds.size.width, height: 30)
        
        //提交动画
        UIView.commitAnimations()
    }
    //点击取消方法
    func cancel(_ item :UIButton) {
        UIView.beginAnimations("cancel", context: nil)
        UIView.setAnimationDuration(0.5)
         tableV.frame = CGRect(x: 0, y: UIScreen.main.bounds.size.height, width: UIScreen.main.bounds.size.width, height: UIScreen.main.bounds.size.height)
        myview.frame = CGRect(x: 0, y: 30, width: UIScreen.main.bounds.size.width, height: 30)
        //提交动画
        UIView.commitAnimations()
    }
   
    //MARK: - UITabelViewDataSource
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 6
    }
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        
        let cell = UITableViewCell(style: .default, reuseIdentifier: "1")
        
        cell.textLabel?.text = citys[indexPath.row]
        
        return cell
        
    }
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        cityBtn .setTitle(citys[indexPath.row], for: .init(rawValue: 0))
        
        UIView.beginAnimations("cancel", context: nil)
        UIView.setAnimationDuration(0.5)
        tableV.frame = CGRect(x: 0, y: UIScreen.main.bounds.size.height, width: UIScreen.main.bounds.size.width, height: UIScreen.main.bounds.size.height)
        myview.frame = CGRect(x: 0, y: 30, width: UIScreen.main.bounds.size.width, height: 30)
        //提交动画
        UIView.commitAnimations()
        
    }
     // MARK: - 高德地图
    override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)
        mapView.showsUserLocation = true
        mapView.userTrackingMode = MAUserTrackingMode.follow
    }
    
    
    func initMapView() {
        
        mapView = MAMapView(frame:(CGRect(x: 0, y: 60, width: UIScreen.main.bounds.size.width, height: UIScreen.main.bounds.size.height)))
        
        self.view.addSubview(mapView!)
    }
    
    func initSearch() {
        // AMap
        search = AMapSearchAPI()
        
        
    }
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }


    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using segue.destinationViewController.
        // Pass the selected object to the new view controller.
    }
    */

}
